#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2007-12-20 - Created
 */
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using SqlServerAdmin;
using IQ.DbBase;

namespace IQ.DbA
{
	/// <summary>
	/// Description of fmStoreProcs.
	/// </summary>
	public partial class FrmStoreProcs : Form
	{
		#region # Constructor # 
		public FrmStoreProcs()
		{
			InitializeComponent();
            grdList.AutoGenerateColumns = false;
		}
		#endregion
		
		#region - Private variables - 
        private int CurrentRow;
		#endregion 

		#region - Private methods - 
        private void paintReservedWords(string[] ResWord, Color clrWord)
        {
            int iPos = 0;
            foreach (string Word in ResWord)
            {
                iPos = reSp.Find(Word, 0, RichTextBoxFinds.WholeWord);
                while (iPos >= 0)
                {
                    reSp.Select(iPos, Word.Length);
                    reSp.SelectionColor = clrWord;
                    iPos = reSp.Find(Word, iPos + 1, RichTextBoxFinds.WholeWord);
                }
            }
        }
        private void paintReservedChars(string chrBegin, string chrEnd, Color clrWord)
        {
            int iB = reSp.Find(chrBegin, 0, RichTextBoxFinds.None);
            int iE = reSp.Find(chrEnd, iB + 1, RichTextBoxFinds.None);
            while (iB >= 0 && iE >= 0)
            {
                reSp.Select(iB, iE - iB + chrEnd.Length);
                reSp.SelectionColor = clrWord;
                iB = reSp.Find(chrBegin, iE + 1, RichTextBoxFinds.None);
                if (iB < iE)
                    iE = -1;
                else
                    iE = reSp.Find(chrEnd, iB + 1, RichTextBoxFinds.None);
            }
        }
        private void progress(object source, SqlServerAdmin.ProgressEvent e)
        {
            pgbProg.Maximum = e.MaxProgress;
            pgbProg.Value = e.Progress;
            pgbProg.PerformStep();
        }
		#endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
		#endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void grdList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			CurrentRow = e.RowIndex;
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
                DataTable datQry = DbHelper.Load_Table(SqlQueries.SqlProcDetail + "OBJECT_ID('" +
				                grdList.Rows[e.RowIndex].Cells[csName.Index].Value.ToString() +"')");
				reSp.Clear();
				reSp.SelectionColor = Color.Black;	
				for (int i = 0; i < datQry.Rows.Count; i++)
				{
					reSp.AppendText(datQry.Rows[i][0].ToString());
				}
                paintReservedWords(SqlConst.sqlReservedWord, SqlConst.clrReservedWord);
                paintReservedWords(SqlConst.sqlReservedFunc, SqlConst.clrReservedFunc);
                paintReservedWords(SqlConst.sqlReservedProc, SqlConst.clrReservedProc);
				//	
				paintReservedChars("'","'",Color.Red);
				paintReservedChars("/*","*/",Color.Brown);
				paintReservedChars("--",char.ConvertFromUtf32(13),Color.Gray);
			}
		}
        private void btnOne_Click(object sender, EventArgs e)
        {
            SqlServerAdmin.SqlDecryptObject DSP = new SqlServerAdmin.SqlDecryptObject((SqlConnection)DbHelper.GetConnection());
            DSP.ObjectName = grdList.Rows[CurrentRow].Cells["NAME"].FormattedValue.ToString();
            DSP.DecryptSP();
            reSp.Clear();
            reSp.Text = DSP.LastError + "\n";
            reSp.AppendText(DSP.DecryptedSql);
        }
        private void btnDoAll_Click(object sender, EventArgs e)
        {
            SqlServerAdmin.SqlDecryptObj DSP = new SqlServerAdmin.SqlDecryptObj((SqlConnection)DbHelper.GetConnection());
            DSP.DoProgress += new SqlServerAdmin.ProgressEventHandler(progress);
            DSP.DecryptAll();
            reSp.Clear();
            reSp.Text = DSP.LastError + "\n" + "Done all!";
        }
		#endregion		

        private void FrmStoreProcs_Load(object sender, EventArgs e)
        {
            if (DbHelper.CanConnect)
            {
                DataTable dtSp = DbHelper.Load_Table(SqlQueries.SqlProcedures);
                grdList.DataSource = dtSp;
            }
        }
		
	}
}
